package main;

public class AppApk {
	private boolean backupError = false;
	private boolean selected = false;
	private boolean data = false;
	private boolean systenApp = false;
	private boolean inSD = false;
	//Path to the apk (/data/app/)
	private String pathApk;
	//Name of the apk file (myApp.apk)
	private String nameApk;
	//Name of the package (com.my.app)
	private String packageName;
	//Path to the apk data folder (com.my.app) (in the folder /data/data/
	private String packageDataPath;
	//Name of the apk (MyApp)
	private String name;

	public AppApk(String appPath, String appPackage, String apk) {
		pathApk = appPath;
		packageName = appPackage;
		nameApk = apk;
		name = nameApk;
		packageDataPath = packageName;// defineDataPath(pathApk, packageName);
		systenApp = isSystemApp(pathApk);
		inSD = isInSD(pathApk);
	}

	/**
	 * 
	 * @param appPath
	 * @param appPackage
	 * 
	 * @deprecated
	 */
	public AppApk(String appPath, String appPackage) {
		pathApk = appPath;
		packageName = appPackage;
		nameApk = packageName;
		packageDataPath = defineDataPath(pathApk, packageName);
		systenApp = isSystemApp(pathApk);
	}

	private boolean isSystemApp(String appPath) {
		return (appPath.indexOf("/system") >= 0);
	}

	private boolean isInSD(String appPath) {
		if ((appPath.indexOf("/system") >= 0)
				|| (appPath.indexOf("/data") >= 0)) {
			return false;
		}
		return true;
	}

	/**
	 * 
	 * @param appPath
	 * @param packageName
	 * @return
	 * @deprecated
	 */
	private String defineDataPath(String appPath, String packageName) {
		if (packageName.equalsIgnoreCase("pkg.apk")) {
			return "";
		} else {
			int firstIndex = 0;
			int lastIndex = packageName.indexOf('-');
			if (lastIndex < 0) {
				lastIndex = packageName.indexOf('.');
			}
			return packageName.substring(firstIndex, lastIndex);
		}
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if (pathApk.equals(((AppApk) obj).getPathApk())) {
			if (packageName.equalsIgnoreCase(((AppApk) obj).getPackageName())) {
				return true;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return name;
	}

	public String getPathApk() {
		return pathApk;
	}

	public void setPathApk(String pathApk) {
		this.pathApk = pathApk;
	}

	public String getNameApk() {
		return nameApk;
	}

	public void setNameApk(String nameApk) {
		this.nameApk = nameApk;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public String getPackageDataPath() {
		return packageDataPath;
	}

	public void setPackageDataPath(String packageDataPath) {
		this.packageDataPath = packageDataPath;
	}

	public boolean isData() {
		return data;
	}

	public void setData(boolean data) {
		this.data = data;
	}

	public boolean isSystenApp() {
		return systenApp;
	}

	public void setSystenApp(boolean systenApp) {
		this.systenApp = systenApp;
	}

	/**
	 * @return the inSD
	 */
	public boolean isInSD() {
		return inSD;
	}

	/**
	 * @param inSD
	 *            the inSD to set
	 */
	public void setInSD(boolean inSD) {
		this.inSD = inSD;
	}

	/**
	 * @return the backupError
	 */
	public boolean isBackupError() {
		return backupError;
	}

	/**
	 * @param backupError the backupError to set
	 */
	public void setBackupError(boolean backupError) {
		this.backupError = backupError;
	}
}
